package com.ruoyi.business.controller;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.business.domain.Processcarddetail;
import com.ruoyi.business.service.IProcesscarddetailService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 工艺文件明细Controller
 * 
 * @author DL
 * @date 2025-06-30
 */
@RestController
@RequestMapping("/processcarddetail")
public class ProcesscarddetailController extends BaseController
{
    @Autowired
    private IProcesscarddetailService processcarddetailService;

    /**
     * 查询工艺文件明细列表
     */
    @GetMapping("/LaserPowerSet")
    public TableDataInfo LaserPowerSet(Processcarddetail processcarddetail)
    {
        List<Processcarddetail> list = processcarddetailService.selectProcesscarddetailList(processcarddetail);
        Map<String, Object> combinedMap = list.stream()
                .filter(detail -> StringUtils.isNotBlank(detail.getParameterCode()))
                .filter(detail -> detail.getSetValue() != null)
                .filter(detail -> "LaserPowerSet".equals(detail.getParameterCode()))
                .collect(Collectors.toMap(
                        Processcarddetail::getParameterCode,
                        Processcarddetail::getSetValue
                ));
        return getDataTable(Collections.singletonList(combinedMap));
    }

    @GetMapping("/list")
    public TableDataInfo list(Processcarddetail processcarddetail) {
        List<Processcarddetail> list = processcarddetailService.selectProcesscarddetailList(processcarddetail);

        Map<String, Object> combinedMap = list.stream()
                .filter(detail -> StringUtils.isNotBlank(detail.getParameterCode()))
                .filter(detail -> !"LaserPowerSet".equals(detail.getParameterCode()))  // Exclude "LaserPowerSet"
                .filter(detail -> detail.getSetValue() != null)  // 添加这行过滤null值
                .collect(Collectors.toMap(
                        Processcarddetail::getParameterCode,
                        Processcarddetail::getSetValue
                ));
        return getDataTable(Collections.singletonList(combinedMap));
    }

    /**
     * 导出工艺文件明细列表
     */
    //@RequiresPermissions("business:processcarddetail:export")
    @Log(title = "工艺文件明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Processcarddetail processcarddetail)
    {
        List<Processcarddetail> list = processcarddetailService.selectProcesscarddetailList(processcarddetail);
        ExcelUtil<Processcarddetail> util = new ExcelUtil<Processcarddetail>(Processcarddetail.class);
        util.exportExcel(response, list, "工艺文件明细数据");
    }

    /**
     * 获取工艺文件明细详细信息
     */
    //@RequiresPermissions("business:processcarddetail:query")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") String ID)
    {
        return success(processcarddetailService.selectProcesscarddetailByID(ID));
    }

    /**
     * 新增工艺文件明细
     */
    //@RequiresPermissions("business:processcarddetail:add")
    @Log(title = "工艺文件明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Processcarddetail processcarddetail)
    {
        return toAjax(processcarddetailService.insertProcesscarddetail(processcarddetail));
    }

    /**
     * 修改工艺文件明细
     */
    //@RequiresPermissions("business:processcarddetail:edit")
    @Log(title = "工艺文件明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Processcarddetail processcarddetail)
    {
        return toAjax(processcarddetailService.updateProcesscarddetail(processcarddetail));
    }

    /**
     * 修改工艺文件明细
     */
    @Log(title = "工艺文件明细", businessType = BusinessType.UPDATE)
    @PutMapping("/editLaserPowerSet")
    public AjaxResult editLaserPowerSet(@RequestBody Processcarddetail processcarddetail)
    {
        return toAjax(processcarddetailService.updateEditLaserPowerSet(processcarddetail));
    }

    /**
     * 修改工艺文件明细SetFlag
     */
    @Log(title = "工艺文件明细", businessType = BusinessType.UPDATE)
    @PutMapping("/editSetFlag")
    public AjaxResult editSetFlag()
    {
        return toAjax(processcarddetailService.updateProcesscarddetailSetFlag());
    }

    /**
     * 删除工艺文件明细
     */
    //@RequiresPermissions("business:processcarddetail:remove")
    @Log(title = "工艺文件明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable String[] IDs)
    {
        return toAjax(processcarddetailService.deleteProcesscarddetailByIDs(IDs));
    }
}
